$(function () {
    $.getJSON("GetFAQ", function (result) {
        page = result;
        for (var i = 0; i < result.length; i++) {
            answer[i] = result[i]["Answer"];
            page[i]["Desc"] = "第" + (i + 1) + "题: " + result[i]["Desc"]
        }
        $(".option").each(function (e) {
            var btn_click = $(this);
            var btn = btn_click[0];
            var index = btn.value;
            btn.title = result[index]["Id"];
            if (result[index]["Answer"] !== 0) {
                $(this).removeClass("btn-default");
                $(this).removeClass("btn-info");
                if (result[index]["Answer"] === result[index]["Correct"]) {
                    $(this).addClass("btn-success");
                } else {
                    $(this).addClass("btn-danger");
                }
            }
            btn_click.click(function () {
                $("#question").text(page[index]["Desc"]);
                $("#optionarea")[0].innerHTML = "";
                $("#optionarea")[0].title = (index);
                if (result[index]["Image"] !== null) {
                    $("#questionimage").fadeIn(500);
                    $("#questionimage").attr("src", result[index]["Image"]);
                } else {
                    $("#questionimage").fadeOut(500)
                }
                $(this).removeClass("btn-default");
                $(this).addClass("btn-info");
                $(".option").each(function (iiii, ee) {
                    if ((page[iiii]["Correct"] === answer[iiii]) && answer[iiii] !== 0) {
                        $(this).addClass("btn-success");
                        $(this).removeClass("btn-info");
                    } else if (answer[iiii] !== 0) {
                        $(this).addClass("btn-danger");
                        $(this).removeClass("btn-info");
                    }
                    if (iiii !== e) {
                        $(this).removeClass("btn-info");
                        $(this).addClass("btn-default");
                    } else {
                        $(this).removeClass("btn-default");
                        $(this).removeClass("btn-danger");
                        $(this).removeClass("btn-success");
                        $(this).addClass("btn-info");
                    }
                });
                var num = ["A:", "B:", "C:", "D:", "E:", "F:", "G:"];
                for (k in result[index]["Option"]) {
                    var isCheck = "";
                    var canClick = "'Answer(this," + k + ")'";
                    var enable = "";
                    if (answer[index] === -1 || cananswer === false) {
                        canClick = "Warning()";
                        enable = "disabled"
                    } else if (answer[index] > 0) {
                        canClick = "";
                        enable = "disabled"
                    }
                    if (answer[index] === parseInt(k)) {
                        isCheck = "checked"
                    }
                    $("#optionarea").append("<div disabled='' onclick=" + canClick + " title=" + index + " class='choose' style='width: 100%;'> <input class='choosen myradio' " + enable + " type='radio' name='mymymy' id='radio" + k + "' value='option1'" + isCheck + "><label for='radio" + k + "'>" + num[k - 1] + result[index]["Option"][k] + "</label> </div> <br>")
                }
                $(".choose").each(function (i, ele) {
                    if (answer[index] !== 0) {
                        if (i + 1 === page[index]["Correct"]) {
                            $(ele).css("background-color", "#98FB98")
                        } else if (i + 1 === answer[index]) {
                            $(ele).css("background-color", "red")
                        }
                    }
                });
            });
            if (e === 0) {
                $(this).click();
            }
        })
    })
});
var answer = [];
var page = null;

function Answer(obj, option_index) {
    answer[obj.title] = option_index;
    console.log("回答中...");

    if (page[obj.title]["Correct"] === option_index) {
        console.log("回答正确");
        nextquestion()
    } else {
        console.log("回答错误");
        $("#reason").text(page[obj.title]["Reason"]);
        $("#memo").fadeIn();
        // $(".choose")[page[obj.title]["Correct"]].css("background-color","#FF0000")
        $(".choose").each(function (i, ele) {
            if (i + 1 === page[obj.title]["Correct"]) {
                $(this).css("background-color", "#98FB98")
            } else if (i + 1 === option_index) {
                $(this).css("background-color", "red")
            }
            $(this).removeAttr("onclick");
        });
        $(".choosen").each(function (i, ele) {
            if (i + 1 !== option_index) {
                $(this).attr("disabled", true)
            }
        })

    }


}

function Warning() {
    alert("已跳过的题无法补答")
}


function lastquestion() {
    var index = $("#optionarea")[0].title;
    $(".option").each(function (i) {
        if (i === parseInt(index) - 1) {
            $(this).click();
            $(this).removeClass("btn-default");
            $(this).addClass("btn-info")
        } else if (index >= 1) {
            $(this).addClass("btn-default");
            $(this).removeClass("btn-info")
        }
    })
}

function nextquestion() {
    var index = $("#optionarea")[0].title;
    if (answer[index] === 0 && cananswer) {

    } else if (answer[index] === -1) {
        AnswerQuestion(-1)
    } else {
        AnswerQuestion(1)
    }
}

function jump() {
    var index = $("#optionarea")[0].title;
    if (answer[index] === 0 && cananswer) {
        answer[index] = 0;
        AnswerQuestion(-1)
    } else {
        AnswerQuestion(1)
    }

}

$(function () {
    $("#takeoff").click(function (e) {
        $("#tips").fadeIn()
    })
});

$(function () {
    $("#examnotover").click(function (e) {
        $("#tips").fadeOut()
    })
});

//答题结果显示
$(function () {
    $("#examover").click(function (e) {
        Examover()
    })
});


function Examover() {
    $("#tips").slideUp(0);
    $.post("submit", {
        pid: pid
    }, function (result) {
        if (result["success"] === true) {
            $(".option").attr("disabled", false);
            $("#result").fadeIn(500);
            $("#takeoff").attr("disabled", true);
            $("#point").text(result["point"] + "分");
            $("#resultstr").text(result["str"]);
            cananswer = false;
            window.clearInterval(ttt)
        }
    })
}

var ttt = null;

function AnswerQuestion(num) {
    var index = $("#optionarea")[0].title;
    var ans = answer[index];
    if (num === -1) {
        ans = 0
    }
    if (!cananswer) {
        $(".option").each(function (i) {
            if (i === parseInt(index) + 1) {
                $(this).click();
                $(this).removeClass("btn-default");
                $(this).addClass("btn-info")
            } else if (index < $(".option").length - 1) {
                $(this).addClass("btn-default");
                $(this).removeClass("btn-info")
            }
        })
    } else {
        $.post("answer", {
            qid: index,
            ans: ans
        }, function (result) {
            if (result["success"] === true) {
                $(".option").each(function (i) {
                    if (i === parseInt(index)) {
                        if (result["correct"] === true) {
                            $(this).removeClass("btn-danger");
                            $(this).removeClass("btn-default");
                            $(this).addClass("btn-success");
                            if (0 === answer[parseInt(index) + 1]) {
                                nextquestion()
                            }
                        } else {
                            // $(this).removeClass("btn-default");
                            // $(this).addClass("btn-danger");
                            return
                        }
                    }
                    if (i === parseInt(index) + 1) {
                        $(this).click();
                        $(this).removeClass("btn-default");
                        $(this).addClass("btn-info")
                    } else if (index < $(".option").length - 1) {
                        $(this).addClass("btn-default");
                        $(this).removeClass("btn-info")
                    }
                })
            } else {
                alert("答题失败,请重试!")
            }
        });
    }
}

var cananswer = true;
var pid = 0;
var timecount = 45 * 60;
$(function () {
    window.clearInterval(ttt);
    $.post("State", function (result) {
        if (parseInt(result["state"]) !== 1) {
            // $(".option").attr("disabled", false);
            cananswer = false;
            $("#memoclose").click(function (e) {
                window.location.href = "/"
            })
        } else {


            $(".option").each(function (e) {
                if (answer[e - 1] !== 0) {
                    $(this).click();
                    if (e >= 56) {
                        $("#cccc").scrollTop(999)
                    }
                }
            });

            $("#memoclose").click(function (e) {

                if (localStorage.getItem("count") > 0) {
                    timecount = localStorage.getItem("count")
                } else {
                    localStorage.setItem("count", timecount)
                }
                if (ttt === null) {
                    ttt = setInterval(function () {
                        timecount--;
                        localStorage.setItem("count", timecount);
                        $("#timecount").text(Math.floor(timecount / 60) + "分" + (timecount % 60) + "秒");
                        if (timecount === 0) {
                            window.clearInterval(ttt);
                            alert("考试时间到!");
                            Examover();
                        }
                    }, 1000);
                }
                $("#memo").fadeOut()
            })
        }
    });
    $("#sure").click(function () {
        $("#result").fadeOut()
    })
});

var video = document.getElementById('video'),
    vendorUrl = window.URL || window.webkitURL;

//媒体对象
navigator.getMedia = navigator.getUserMedia ||
    navagator.webkitGetUserMedia ||
    navigator.mozGetUserMedia ||
    navigator.msGetUserMedia;
navigator.getMedia({
    video: true, //使用摄像头对象
    audio: false  //不适用音频
}, function (strem) {
    video.src = vendorUrl.createObjectURL(strem);
    video.play();
}, function (error) {
    console.log(error);
});
